package com.jaydenxiao.common.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by hasee on 2017/10/7.
 */

public abstract class BaseFragment<V extends ViewDataBinding> extends Fragment {

    protected V dataBinding;
    protected BaseActivity mActivity;
    private ProgressDialog dialog;
    protected DetachListener detachListener;
    protected CompositeDisposable disposables;
    protected abstract int getLayoutId();
    protected abstract void initView();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        dataBinding =DataBindingUtil.inflate(inflater,getLayoutId(),container,false);
        initView();
        if (detachListener==null){
            disposables=new CompositeDisposable();
        }
        return dataBinding.getRoot();

    }




    public void removeFragment(){
        if (mActivity.getSupportFragmentManager().getBackStackEntryCount()>=1){
            mActivity.getSupportFragmentManager().popBackStack();
        }else {
            mActivity.finish();
        }
    }


    public void addFragment(Fragment from,Fragment to){
        mActivity.addFragment(from,to);

    }

//
//    public void switchFragment(BaseFragment to){
//        mActivity.switchFragment(to);
//    }


    public void backToSpecifiedFragment(String fragmentName){
        int fragmentCount=mActivity.getSupportFragmentManager().getBackStackEntryCount();
        for (int i=0;i<fragmentCount;i++){
            FragmentManager.BackStackEntry entry=mActivity.getSupportFragmentManager().getBackStackEntryAt(i);
            if (entry.getName().equals(fragmentName)){
                break;
            }
            mActivity.getSupportFragmentManager().popBackStackImmediate();
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity= (BaseActivity) context;
    }


    @Override
    public void onDestroyView() {
        if (dataBinding !=null){
            dataBinding.executePendingBindings();
            dataBinding.unbind();
        }
        super.onDestroyView();
    }

    public void showProgressDialog(){
        if (dialog==null){
            dialog=new ProgressDialog(mActivity);
        }
        if (!dialog.isShowing()){
            dialog.show();
        }
    }

    public void dismissDialog(){
        if (dialog==null){
            return;
        }
        if (!dialog.isShowing()){
            return;
        }
        dialog.dismiss();
    }


    public void setProgress(int progress){
        if (dialog==null){
            return;
        }
        if (!dialog.isShowing()){
            return;
        }
        dialog.setProgress(progress);
    }

    public interface DetachListener{
        void detach();
    }


    protected void addDisposable(Disposable disposable){
        if (disposables!=null){
            disposables.add(disposable);
        }
    }

    protected void clearDisposable(){
        if (disposables==null){
            return;
        }
        disposables.clear();
        disposables=null;
    }


    @Override
    public void onDestroy() {
        if (detachListener!=null){
            detachListener.detach();
        }
        clearDisposable();
//        RefWatcher refWatcher = BaseApplication.getRefWatcher(mActivity);
//        refWatcher.watch(this);
        super.onDestroy();
    }



}
